package shaka.ui

import org.eclipse.swt.SWT
import org.eclipse.swt.events.MenuDetectEvent
import org.eclipse.swt.events.SelectionEvent
import org.eclipse.swt.events.ShellAdapter
import org.eclipse.swt.events.ShellEvent
import org.eclipse.swt.graphics.Image
import org.eclipse.swt.layout.GridData
import org.eclipse.swt.widgets.Display
import org.eclipse.swt.widgets.Shell

import shaka.groovy.swt.SwtBuilder

public class SwtExample {
	static example1(){
		String fontType  = 'Consolas'
		int    fontSize  = 14
		def    fontStyle = SWT.NONE

		SwtBuilder swt = new SwtBuilder()
		swt.shell(id:'shell', text:'ex1'){
			gridLayout(c:[2, false])

			label(c:[SWT.NONE], text:'A label'){ font(c:[fontType, fontSize, fontStyle]) }
			button(c:[SWT.PUSH], text:'Press Me'){
				event(click:{SelectionEvent e->
					def shell = swt.shell(parent:swt.id['shell'], c:[SWT.ON_TOP | SWT.DIALOG_TRIM], text:'Dialog', size:[200, 200])
					swt.setLocation(shell, 0.5, 0.5)
					shell.open()
				})
				font(c:[fontType, fontSize, fontStyle])
			}

			label(c:[SWT.BORDER], text:'This is a label'){
				font(c:[fontType, fontSize, fontStyle])
				gridData(c:[GridData.FILL, GridData.BEGINNING, true, false, 2, 1])
			}

			label(c:[SWT.SEPARATOR | SWT.HORIZONTAL]){
				gridData(
						c:[GridData.FILL, GridData.BEGINNING, true, false, 2, 1],
						prop:[horizontalSpan:2])
			}

			button(c:[SWT.PUSH], text:'New Button'){
				font(c:[fontType, fontSize, fontStyle])
				gridData(c:[GridData.END, GridData.BEGINNING, false, false, 2, 1])
			}

			spinner(c:[SWT.READ_ONLY], minimum:0, maximum:1000, selection:500, increment:1, pageIncrement:100){
				font(c:[fontType, fontSize, fontStyle])
				gridData(
						c:[SWT.FILL, SWT.FILL, true, false],
						prop:[widthHint:SWT.DEFAULT, heightHint:SWT.DEFAULT, horizontalSpan:2])
			}

			composite(c:[SWT.BORDER]){
				gridData(
						c:[SWT.FILL, SWT.FILL, true, false],
						prop:[horizontalSpan:2])
				gridLayout(c:[1, false])

				text(c:[SWT.NONE], text:'Testing'){
					font(c:[fontType, fontSize, fontStyle])
					gridData(c:[SWT.FILL, SWT.FILL, true, false])
				}

				text(c:[SWT.NONE], text:'Another test'){ font(c:[fontType, fontSize, fontStyle]) }
			}

			group(c:[SWT.NONE], text:'This is my group'){
				font(c:[fontType, fontSize, fontStyle])
				gridData(
						c:[SWT.FILL, SWT.FILL, true, false],
						prop:[horizontalSpan:2])
				rowLayout()

				text(c:[SWT.NONE], text:'Another test'){ font(c:[fontType, fontSize, fontStyle]) }
			}

			pack()
		}
		swt.setLocation(swt.id['shell'], 0.2, 0.2)
		swt.show()
	}

	static example2(){
		String fontType  = 'Consolas'
		int    fontSize  = 14
		def    fontStyle = SWT.NONE

		def shellDisplay = { Shell shell, boolean visible ->
			shell.setVisible(visible) // 控制窗口顯示
			// 如果窗口是顯示狀態
			if (shell.getVisible()) {
				shell.setMinimized(false) // 阻止窗口最小化
				shell.setActive() // 激活窗口
			}
		}

		SwtBuilder swt = new SwtBuilder()
		Display display = swt.id['display']
		Image icon = display.getSystemImage(SWT.ICON_INFORMATION)
		swt.shell(
				id:'shell', text:'ex2', image:icon,
				addShellListener:[
					shellIconified:{ShellEvent e->
						// 最小化事件
						Shell shell = swt.id['shell']
						shellDisplay(shell, false)
					},
					shellClosed:{ShellEvent e->
						// 關閉事件
						e.doit = false // 取消關閉操作
						Shell shell = swt.id['shell']
						shellDisplay(shell, false)
					}] as ShellAdapter){

					gridLayout()
					cTabFolder(c:[SWT.BOTTOM]){
						gridData(c:[GridData.FILL, GridData.FILL, true, true, 1, 1])
						cTabItem(c:[SWT.NONE], text:'Tab1'){
							font(c:[fontType, fontSize, fontStyle])
							text(c:[SWT.MULTI | SWT.BORDER], text:'Hello1'){ font(c:[fontType, fontSize, fontStyle]) }
						}
						cTabItem(c:[SWT.NONE], text:'Tab2'){
							font(c:[fontType, fontSize, fontStyle])
							text(c:[SWT.MULTI | SWT.BORDER], text:'Hello2'){ font(c:[fontType, fontSize, fontStyle]) }
						}
						cTabItem(c:[SWT.NONE], text:'Tab3'){
							font(c:[fontType, fontSize, fontStyle])
							text(c:[SWT.MULTI | SWT.BORDER], text:'Hello3'){ font(c:[fontType, fontSize, fontStyle]) }
						}
					}

					trayItem(
							id:'trayItem', c:[SWT.NONE],
							image:icon,
							visible:true, ToolTipText:swt.id['shell'].text){
								event(click:{SelectionEvent e->
									// 左鍵 - 顯示 trayMenu
									swt.id['trayMenu'].setVisible(true)
								}, rclick:{MenuDetectEvent e->
									// 右鍵 - 顯示 trayMenu
									swt.id['trayMenu'].setVisible(true)
								})
								menu(id:'trayMenu', parent:swt.id['shell'], c:[SWT.POP_UP]){
									menuItem(c:[SWT.PUSH], text:'顯示窗口(&s)'){
										event(click:{SelectionEvent e->
											Shell shell = swt.id['shell']
											shellDisplay(shell, true)
										})
									}
									menuItem(c:[SWT.PUSH], text:'退出程式(&x)'){
										event(click:{SelectionEvent e->
											Shell shell = swt.id['shell']
											shell.dispose()
										})
									}
								}
							}
					pack()
				}
		swt.setLocation(swt.id['shell'], 1, 0.93)
		swt.show()
	}

	public static void main(String...args){
		example1()
	}
}
